

'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');

const db=require(path.join(__dirname,"../configs/database.js"));

const base=require(path.join(__dirname,"./baseController.js"));

module.exports.workList=function(req,res){
  const page=parseInt(req.query.page||'1');
  const limit=parseInt(req.query.limit||'12');
  const start=(page-1)*limit;

  db.table("homework").count("*").then(function(count){

     const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
     const totalPageArray=[];
     
     for(var i=1;i<=totalPage;i++){
         totalPageArray.push(i);
     }

  db.table("homework").join("pre_class on pre_homework.class_id = pre_class.class_id").order("work_id DESC").limit(start,limit).select().then(function(data){
   xtpl.renderFile(
    path.join(__dirname,"../views/admin/stu_work_list.html"),
    {
    	array:data,
    	totalPage:totalPage,
        totalPageArray:totalPageArray,
        page:page
    },
    function(err,content){
        if(err){
           return console.log(err);
        }
        res.setHeader("Content-Type","text/html;charset=utf-8");
        res.end(content);
    }

  	);
     });


  });

};
